package com.blockmeta.bbs.overallserviceapplication.search.adapter;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.net.Uri;
import androidx.annotation.Nullable;
import android.text.Layout;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;

import android.text.style.StyleSpan;
import android.view.View;
import android.widget.TextView;

import com.blankj.utilcode.util.SpanUtils;
import com.blockmeta.bbs.baselibrary.base.adapter.BaseAdapter;
import com.blockmeta.bbs.baselibrary.utils.FormatUtil;
import com.blockmeta.bbs.baselibrary.utils.RegexUtil;
import com.blockmeta.bbs.baselibrary.utils.ToastUtil;
import com.blockmeta.bbs.businesslibrary.pojo.FastListPOJO;
import com.blockmeta.bbs.businesslibrary.widget.CircleImageView;
import com.blockmeta.bbs.overallserviceapplication.R;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by author on 2018/1/18.
 */

public class FastListAdapter extends BaseAdapter<FastListPOJO.ListBean, BaseViewHolder> {
    private Context mContext;
    private OnSharedClickListener mOnSharedClickListener;
    private String searchKeyWord;

    public void setOnSharedClickListener(OnSharedClickListener onSharedClickListener) {
        mOnSharedClickListener = onSharedClickListener;
    }

    public interface OnSharedClickListener {
        void onShareClicked(FastListPOJO.ListBean item);
    }

    public FastListAdapter(int layoutResId, @Nullable List<FastListPOJO.ListBean> data, Context context) {
        super(layoutResId, data);
        this.mContext = context;
    }

    public void setSearchKeyWord(String searchKeyWord) {
        this.searchKeyWord = searchKeyWord;
    }

    private void jumpToWeb(String url) {
        if (TextUtils.isEmpty(url)) {
            return;
        }
        Intent intent = new Intent();
        intent.setAction("android.intent.action.VIEW");
        Uri uri = Uri.parse(url);
        intent.setData(uri);
        boolean isHaveProcessor=intent.resolveActivity(mContext.getPackageManager())!=null;
        if (isHaveProcessor) {
            mContext.startActivity(intent);
        }
    }
    String regex = "(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]";
    Pattern mUrlPattern = Pattern.compile(regex);
    @Override
    protected void convert(BaseViewHolder helper, final FastListPOJO.ListBean item) {
        helper.setText(R.id.fast_time, FormatUtil.getTimetoHMS(Integer.parseInt(item.getPost_date()), "yyyy-MM-dd HH:mm"));
        final TextView spanText = helper.getView(R.id.fast_webview);
        String bold = "【" + item.getTitle() + "】";
        String content = item.getContent()==null?"":item.getContent();
        Matcher matcher = mUrlPattern.matcher(content);
        int titleLen = bold.length();
        SpannableStringBuilder stringBuilder = new SpannableStringBuilder(bold).append(content);
        stringBuilder.setSpan(new StyleSpan(Typeface.BOLD),0,titleLen,Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        spanText.setMovementMethod(LinkMovementMethod.getInstance());
        while(matcher.find()){
            final String url = matcher.group();
            int start = matcher.start()+titleLen;
            int end = matcher.end()+titleLen;
            stringBuilder.setSpan(new ClickableSpan() {
                @Override
                public void onClick(View widget) {
                    jumpToWeb(url);
                }

                @Override
                public void updateDrawState(TextPaint ds) {
                    ds.setColor(mContext.getResources().getColor(R.color.base_blue));
                    ds.setUnderlineText(false);
                }
            },start,end, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        }

        spanText.setText(RegexUtil.getHightLight(stringBuilder, searchKeyWord));


        final TextView textView = new TextView(mContext);
        textView.setText(new SpanUtils()
                .append(spanText.getText().toString())
                .appendLine()
                .appendLine()
                .append("——来自趣会APP").setAlign(Layout.Alignment.ALIGN_OPPOSITE)
                .appendLine()
                .append("以价值投资为导向").setAlign(Layout.Alignment.ALIGN_CENTER)
                .create()
        );
        spanText.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                ClipboardManager cm = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
                ClipData mClipData = ClipData.newPlainText("复制文字", textView.getText().toString());
//                // 将ClipData内容放到系统剪贴板里。
                cm.setPrimaryClip(mClipData);
                ToastUtil.show(mContext, "您已复制");
                return true;
            }
        });

        helper.setText(R.id.fast_from, item.getSource().getName());
        CircleImageView view = helper.getView(R.id.fast_img);
        if (item.getSource().getIco() != null && item.getSource().getIco().startsWith("http")) {
            Glide.with(mContext).load(item.getSource().getIco()).into(view);
        } else {
            view.setVisibility(View.GONE);
        }
        helper.setText(R.id.fast_share, "分享").addOnClickListener(R.id.fast_share);
        TextView textView1 = helper.getView(R.id.fast_share);
        textView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnSharedClickListener == null) return;
                mOnSharedClickListener.onShareClicked(item);
            }
        });
    }
}
